﻿using System;
using System.Drawing;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;

namespace MyMap
{
    public partial class RenderSettingsForm : Form
    {
        private IFeatureLayer _featureLayer;
        private esriGeometryType _geometryType;

        public RenderSettingsForm(IFeatureLayer featureLayer)
        {
            InitializeComponent();
            _featureLayer = featureLayer;

            // 获取几何类型
            IFeatureClass featureClass = featureLayer.FeatureClass;
            _geometryType = featureClass.ShapeType;

            InitializeForm();
        }

        private void InitializeForm()
        {
            // 根据几何类型显示不同的控件
            switch (_geometryType)
            {
                case esriGeometryType.esriGeometryPoint:
                    ShowPointControls();
                    break;
                case esriGeometryType.esriGeometryPolyline:
                    ShowLineControls();
                    break;
                case esriGeometryType.esriGeometryPolygon:
                    ShowPolygonControls();
                    break;
                default:
                    MessageBox.Show("不支持的几何类型！");
                    this.Close();
                    break;
            }
        }

        private void ShowPointControls()
        {
            this.Text = "点图层渲染设置";
            this.Height = 300;

            // 点大小
            Label lblSize = new Label() { Text = "点大小:", Left = 20, Top = 20, Width = 80 };
            NumericUpDown numSize = new NumericUpDown() { Left = 100, Top = 20, Width = 60, Minimum = 1, Maximum = 50, Value = 8 };

            // 点颜色
            Label lblColor = new Label() { Text = "点颜色:", Left = 20, Top = 60, Width = 80 };
            Button btnColor = new Button() { Text = "选择颜色", Left = 100, Top = 60, Width = 80, BackColor = Color.Red };
            btnColor.Click += (s, e) => { btnColor.BackColor = ShowColorDialog(btnColor.BackColor); };

            // 点样式
            Label lblStyle = new Label() { Text = "点样式:", Left = 20, Top = 100, Width = 80 };
            ComboBox cmbStyle = new ComboBox() { Left = 100, Top = 100, Width = 120 };
            cmbStyle.Items.AddRange(new string[] { "圆形", "正方形", "十字形", "三角形" });
            cmbStyle.SelectedIndex = 0;

            // 确定按钮
            Button btnOK = new Button() { Text = "确定", Left = 60, Top = 150, Width = 80 };
            btnOK.Click += (s, e) => ApplyPointRender((double)numSize.Value, btnColor.BackColor, cmbStyle.SelectedIndex);

            // 取消按钮
            Button btnCancel = new Button() { Text = "取消", Left = 160, Top = 150, Width = 80 };
            btnCancel.Click += (s, e) => this.Close();

            this.Controls.AddRange(new Control[] { lblSize, numSize, lblColor, btnColor, lblStyle, cmbStyle, btnOK, btnCancel });
        }

        private void ShowLineControls()
        {
            this.Text = "线图层渲染设置";
            this.Height = 300;

            // 线宽度
            Label lblWidth = new Label() { Text = "线宽度:", Left = 20, Top = 20, Width = 80 };
            NumericUpDown numWidth = new NumericUpDown() { Left = 100, Top = 20, Width = 60, Minimum = 0.1M, Maximum = 10, Value = 1, DecimalPlaces = 1, Increment = 0.1M };

            // 线颜色
            Label lblColor = new Label() { Text = "线颜色:", Left = 20, Top = 60, Width = 80 };
            Button btnColor = new Button() { Text = "选择颜色", Left = 100, Top = 60, Width = 80, BackColor = Color.Blue };
            btnColor.Click += (s, e) => { btnColor.BackColor = ShowColorDialog(btnColor.BackColor); };

            // 线样式
            Label lblStyle = new Label() { Text = "线样式:", Left = 20, Top = 100, Width = 80 };
            ComboBox cmbStyle = new ComboBox() { Left = 100, Top = 100, Width = 120 };
            cmbStyle.Items.AddRange(new string[] { "实线", "虚线", "点线", "点划线" });
            cmbStyle.SelectedIndex = 0;

            // 确定按钮
            Button btnOK = new Button() { Text = "确定", Left = 60, Top = 150, Width = 80 };
            btnOK.Click += (s, e) => ApplyLineRender((double)numWidth.Value, btnColor.BackColor, cmbStyle.SelectedIndex);

            // 取消按钮
            Button btnCancel = new Button() { Text = "取消", Left = 160, Top = 150, Width = 80 };
            btnCancel.Click += (s, e) => this.Close();

            this.Controls.AddRange(new Control[] { lblWidth, numWidth, lblColor, btnColor, lblStyle, cmbStyle, btnOK, btnCancel });
        }

        private void ShowPolygonControls()
        {
            this.Text = "面图层渲染设置";
            this.Height = 400;

            // 填充颜色
            Label lblFillColor = new Label() { Text = "填充颜色:", Left = 20, Top = 20, Width = 80 };
            Button btnFillColor = new Button() { Text = "选择颜色", Left = 100, Top = 20, Width = 80, BackColor = Color.Green };
            btnFillColor.Click += (s, e) => { btnFillColor.BackColor = ShowColorDialog(btnFillColor.BackColor); };

            // 边线颜色
            Label lblOutlineColor = new Label() { Text = "边线颜色:", Left = 20, Top = 60, Width = 80 };
            Button btnOutlineColor = new Button() { Text = "选择颜色", Left = 100, Top = 60, Width = 80, BackColor = Color.Black };
            btnOutlineColor.Click += (s, e) => { btnOutlineColor.BackColor = ShowColorDialog(btnOutlineColor.BackColor); };

            // 边线宽度
            Label lblOutlineWidth = new Label() { Text = "边线宽度:", Left = 20, Top = 100, Width = 80 };
            NumericUpDown numOutlineWidth = new NumericUpDown() { Left = 100, Top = 100, Width = 60, Minimum = 0.1M, Maximum = 5, Value = 0.4M, DecimalPlaces = 1, Increment = 0.1M };

            // 填充样式
            Label lblFillStyle = new Label() { Text = "填充样式:", Left = 20, Top = 140, Width = 80 };
            ComboBox cmbFillStyle = new ComboBox() { Left = 100, Top = 140, Width = 120 };
            cmbFillStyle.Items.AddRange(new string[] { "实心填充", "空心填充", "水平线", "垂直线", "交叉线" });
            cmbFillStyle.SelectedIndex = 0;

            // 确定按钮
            Button btnOK = new Button() { Text = "确定", Left = 60, Top = 190, Width = 80 };
            btnOK.Click += (s, e) => ApplyPolygonRender(btnFillColor.BackColor, btnOutlineColor.BackColor,
                (double)numOutlineWidth.Value, cmbFillStyle.SelectedIndex);

            // 取消按钮
            Button btnCancel = new Button() { Text = "取消", Left = 160, Top = 190, Width = 80 };
            btnCancel.Click += (s, e) => this.Close();

            this.Controls.AddRange(new Control[] { lblFillColor, btnFillColor, lblOutlineColor, btnOutlineColor,
                lblOutlineWidth, numOutlineWidth, lblFillStyle, cmbFillStyle, btnOK, btnCancel });
        }

        private Color ShowColorDialog(Color currentColor)
        {
            using (ColorDialog colorDialog = new ColorDialog())
            {
                colorDialog.Color = currentColor;
                if (colorDialog.ShowDialog() == DialogResult.OK)
                {
                    return colorDialog.Color;
                }
                return currentColor;
            }
        }

        private void ApplyPointRender(double size, Color color, int styleIndex)
        {
            try
            {
                ISimpleMarkerSymbol markerSymbol = new SimpleMarkerSymbolClass();
                markerSymbol.Size = size;
                markerSymbol.Color = CreateRGBColor(color);

                // 设置点样式
                switch (styleIndex)
                {
                    case 0: markerSymbol.Style = esriSimpleMarkerStyle.esriSMSCircle; break;
                    case 1: markerSymbol.Style = esriSimpleMarkerStyle.esriSMSSquare; break;
                    case 2: markerSymbol.Style = esriSimpleMarkerStyle.esriSMSCross; break;
                    case 3: markerSymbol.Style = esriSimpleMarkerStyle.esriSMSDiamond; break;
                }

                ISimpleRenderer simpleRenderer = new SimpleRendererClass();
                simpleRenderer.Symbol = markerSymbol as ISymbol;

                IGeoFeatureLayer geoFeatureLayer = _featureLayer as IGeoFeatureLayer;
                geoFeatureLayer.Renderer = simpleRenderer as IFeatureRenderer;

                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show($"应用点渲染失败: {ex.Message}");
            }
        }

        private void ApplyLineRender(double width, Color color, int styleIndex)
        {
            try
            {
                ISimpleLineSymbol lineSymbol = new SimpleLineSymbolClass();
                lineSymbol.Width = width;
                lineSymbol.Color = CreateRGBColor(color);

                // 设置线样式
                switch (styleIndex)
                {
                    case 0: lineSymbol.Style = esriSimpleLineStyle.esriSLSSolid; break;
                    case 1: lineSymbol.Style = esriSimpleLineStyle.esriSLSDash; break;
                    case 2: lineSymbol.Style = esriSimpleLineStyle.esriSLSDot; break;
                    case 3: lineSymbol.Style = esriSimpleLineStyle.esriSLSDashDot; break;
                }

                ISimpleRenderer simpleRenderer = new SimpleRendererClass();
                simpleRenderer.Symbol = lineSymbol as ISymbol;

                IGeoFeatureLayer geoFeatureLayer = _featureLayer as IGeoFeatureLayer;
                geoFeatureLayer.Renderer = simpleRenderer as IFeatureRenderer;

                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show($"应用线渲染失败: {ex.Message}");
            }
        }

        private void ApplyPolygonRender(Color fillColor, Color outlineColor, double outlineWidth, int styleIndex)
        {
            try
            {
                ISimpleFillSymbol fillSymbol = new SimpleFillSymbolClass();
                fillSymbol.Color = CreateRGBColor(fillColor);

                // 设置填充样式
                switch (styleIndex)
                {
                    case 0: fillSymbol.Style = esriSimpleFillStyle.esriSFSSolid; break;
                    case 1: fillSymbol.Style = esriSimpleFillStyle.esriSFSHollow; break;
                    case 2: fillSymbol.Style = esriSimpleFillStyle.esriSFSHorizontal; break;
                    case 3: fillSymbol.Style = esriSimpleFillStyle.esriSFSVertical; break;
                    case 4: fillSymbol.Style = esriSimpleFillStyle.esriSFSCross; break;
                }

                // 设置边线
                ISimpleLineSymbol outlineSymbol = new SimpleLineSymbolClass();
                outlineSymbol.Width = outlineWidth;
                outlineSymbol.Color = CreateRGBColor(outlineColor);
                outlineSymbol.Style = esriSimpleLineStyle.esriSLSSolid;
                fillSymbol.Outline = outlineSymbol;

                ISimpleRenderer simpleRenderer = new SimpleRendererClass();
                simpleRenderer.Symbol = fillSymbol as ISymbol;

                IGeoFeatureLayer geoFeatureLayer = _featureLayer as IGeoFeatureLayer;
                geoFeatureLayer.Renderer = simpleRenderer as IFeatureRenderer;

                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show($"应用面渲染失败: {ex.Message}");
            }
        }

        private IRgbColor CreateRGBColor(Color color)
        {
            IRgbColor rgbColor = new RgbColorClass();
            rgbColor.Red = color.R;
            rgbColor.Green = color.G;
            rgbColor.Blue = color.B;
            return rgbColor;
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // RenderSettingsForm
            // 
            this.ClientSize = new System.Drawing.Size(284, 261);
            this.Name = "RenderSettingsForm";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
            this.ResumeLayout(false);
        }
    }
}